//****************  script for new otc items implementing scriptaculosus ***************
Event.observe(window, 'load', function(){
    new Ajax.Autocompleter("autocomplete_jrv", "autocomplete_choices", "documentServlet",  
    {minChars : 4,
        callback: function(element, entry) {
            return entry + "&ballot=" + $("docForm:ballotsCombo").value + 
                           "&counting=" + $("docForm:counting").value +
                           "&operation=" + $("docForm:operation").value;
        },
        afterUpdateElement : getSelectionId});
});


function getSelectionId(text, li) {
    var st = li.id;
    var fd = text.name;
    new Ajax.Request('documentValidateServlet', {
        method:'get',
        asynchronous: false,
        parameters: {id: st, field : fd},
        onSuccess: function(transport){
            var response = transport.responseText.evalJSON();
            var item = response.junta;
            var frm = 'docForm';
            if(fd == 'autocomplete_jrv'){
                $(frm + ':jrvHidden').value = st;
//                $(frm + ':townsCombo').value = item.townId;
//                $(frm + ':ballotsCombo').value = item.ballotId;
//                alert('Entra '+ item.townId + ' - ' + item.townName + ' - ' + item.ballotDesc);
                $(frm + ':townsCombo').style.visibility = 'hidden';
                $(frm + ':ballotsCombo').style.visibility = 'hidden';
                $(frm + ':townName').innerHTML = item.townName;
                $(frm + ':townName').style.visibility = 'visible';
                $(frm + ':ballotDesc').innerHTML = item.ballotDesc;
                $(frm + ':ballotDesc').style.visibility = 'visible';
            }
        },
        onFailure: function(){    
            alert('Falla de conexion. Pruebe de nuevo.');
        }
        
    });
}
